<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	set_html('title','收款码');
	$sid=$_RQ['sid'];
	if(!$sid){
		exi('请选择收款门店！','error');
	}
	$this->session('storeid',$sid);
	$store=pdo_get('jy_weishop_store',array('id'=>$sid,'uniacid'=>$_SESSION['uniacid']));
	$return['store']=$store;
	$return['paymoney']=100;
	exi($return);
}
if($_RQ['op']=='post'){
		$postdata=get_postdata();
		$paymoney=$postdata['paymoney'];
		if($postdata['paymoney']<=0){
			exi('请输入支付金额！','error');
		}
		$store=pdo_get('jy_weishop_store',array('id'=>$this->session('storeid'),'uniacid'=>$_SESSION['uniacid']));
		$workerid=$this->pdo_getcolumn('member',array('mid'=>$_SESSION['uid']),'workerid');
		$tid='CS'.$TIME.rand(10000,99999);
		$data=array(
			'ordersn'=>'MF'.get_ordersn($this->tablename('member_feelog'),'ordersn'),
			'uniacid'=>$_SESSION['uniacid'],
			'storeid'=>$this->session('storeid'),
			'workerid'=>$workerid,
			'price'=>$paymoney,
			'createtime'=>time(),
			'status'=>0,
			'mid'=>$_SESSION['uid']
		);
		pdo_insert('jy_weishop_member_feelog',$data); 
		$oid=pdo_insertid();	
		$params=array(
			'title'=>'门店《'.$store['title'].'》收款',
			'fee'=>$paymoney,
			'id'=>$oid,
			'plugin'=>'jy_weishop'
		);	
		$params['back_url']='jy_weishop/store.pay/payresult/';
		$return['payid']=cfc('pay')->create_pay_order($params);
		pdo_update('jy_weishop_member_feelog',array('pay_id'=>$return['payid']),array('id'=>$oid));
		exi($return);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('收款失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('收款失败！','error');
	}
	$order=pdo_get('jy_weishop_member_feelog',array('id'=>$payresult['id']));
	if(!$order){
		exi('收款失败！订单不存在！','error');
	}
	if($order['price']!=$payresult['fee']){
		exi('收款失败！订单金额不匹配！','error');
	}
	
	pdo_update('jy_weishop_member_feelog',array('status'=>1,'paytime'=>time(),'paytype'=>$payresult['pay_type'],'transid'=>$payresult['transaction_id']),array('id'=>$order['id']));
	exi('收款成功','success');
}